<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Add_garage extends Page
{
   public $lblCity = null;
   public $Label13 = null;
   public $cmbCountry = null;
   public $edCity = null;
   public $Label11 = null;
   public $JSProvinceMunicipality = null;
   public $JSRegionProvince = null;
   public $lblAddress = null;
   public $lblname = null;
   public $hfError = null;
   public $JSValidate = null;
   public $hiddenRandom = null;
   public $edname = null;
   public $hiddenComp = null;
   public $Image2 = null;
   public $btnContactCancel = null;
   public $btnSubmit = null;
   public $JTSiteTheme1 = null;
   public $cmbMunicipality = null;
   public $cmbProvince = null;
   public $cmbRegion = null;
   public $edBldg = null;
   public $edAddress = null;
   public $Label8 = null;
   public $Label28 = null;
   public $Label27 = null;
   public $Label26 = null;
   public $Label25 = null;
   public $Label24 = null;
   public $Label10 = null;
   function edCityJSBlur($sender, $params)
   {

   ?>
   //Add your javascript code here
   validateRequire('edCity','lblCity');
   <?php

   }

   function Add_garageJSLoad($sender, $params)
   {
   $this->clr_reg();
   ?>
   //Add your javascript code here

   <?php

   }

   function edAddressJSBlur($sender, $params)
   {

?>
       validateRequire('edAddress','lblAddress');
       <?php

   }

   function ednameJSBlur($sender, $params)
   {

?>
       validateRequire('edname','lblname');
       <?php

   }

   function validateRequired($value, $lbl)
   {
      if($value == '' || $value == null)
      {
         $this->$lbl->Caption = '&darr; This field is required &darr;';
         $this->hfError->setValue('1');
      }
   }
   function cmbProvinceJSChange($sender, $params)
   {
      //echo $this->cmbProvince->ajaxCall("VendorProvinceChange");
?>
       provinceMunicipality();
       return false;
       <?php

   }

   function cmbRegionJSChange($sender, $params)
   {
      //echo $this->cmbRegion->ajaxCall("VendorRegionChange");
   ?>
       regionProvince();
       //provinceMunicipality();
       return false;
   <?php

   }


   function clr_reg()
      {
      $this->cmbRegion->Clear();
      $this->cmbRegion->AddItem('--Select--', null , null);
      $this->cmbRegion->setItemIndex('--Select--');
      $result_vend_region = mysql_query("select * from lookup_region");

      while($row_vend_region = mysql_fetch_array($result_vend_region))
      {
         $this->cmbRegion->AddItem($row_vend_region['region'], null , $row_vend_region['id']);
      }

      $this->cmbProvince->Clear();
      $this->cmbProvince->AddItem('--Select Region First--', null , null);
      $this->cmbMunicipality->Clear();
      $this->cmbMunicipality->AddItem('--Select Province First--', null , null);
      }




   function Add_garageCreate($sender, $params)
   {
      $comp_id = $_GET['comp_id'];
      $this->hiddenComp->setValue($comp_id);

      $this->hfError->setValue('0');
      $this->lblname->Caption = '';
      $this->lblAddress->Caption = '';
      $this->lblCity->Caption = '';

      $this->edname->Text = '';
      $this->edAddress->Text = '';
      $this->edBldg->Text = '';


      $this->cmbCountry->Clear();
      $this->cmbCountry->ItemIndex = "Philippines";
        $result_country = mysql_query("select * from countries order by country ASC");

        while($row_country = mysql_fetch_array($result_country))
        {
            $this->cmbCountry->AddItem($row_country['country'], null, $row_country['country']);
        }

      //Vendor Region,Prov, Municipality

   }



   function VendorRegionChange()
   {
      $region_id = $this->cmbRegion->getItemIndex();
      $this->cmbProvince->Clear();
      $this->cmbProvince->AddItem('--Select--', null , null);
      $result_province = mysql_query("select * from lookup_province where region_id='" . $region_id . "'");

      while($row_province = mysql_fetch_array($result_province))
      {
         $this->cmbProvince->AddItem($row_province['province'], null , $row_province['id']);
      }

      if($region_id == 0)
      {
         $this->cmbProvince->Clear();
         $this->cmbMunicipality->Clear();
         $this->cmbProvince->AddItem('--Select Region First--', null , null);
         $this->cmbMunicipality->AddItem('--Select Province First--', null , null);
      }
   }


   function VendorProvinceChange()
   {
      $region_id = $this->cmbRegion->getItemIndex();
      $this->cmbProvince->Clear();
      $this->cmbProvince->AddItem('--Select--', null , null);
      $result_province = mysql_query("select * from lookup_province where region_id='" . $region_id . "'");

      while($row_province = mysql_fetch_array($result_province))
      {
         $this->cmbProvince->AddItem($row_province['province'], null , $row_province['id']);
      }

      $province_id = $this->cmbProvince->getItemIndex();
      $this->cmbMunicipality->Clear();
      $this->cmbMunicipality->AddItem('--Select--', null , null);
      $result_municipality = mysql_query("select * from lookup_municipality where province_id='" . $province_id . "'");

      while($row_municipality = mysql_fetch_array($result_municipality))
      {
         $this->cmbMunicipality->AddItem($row_municipality['municipality'], null , $row_municipality['id']);
      }

      if($province_id == 0)
      {
         $this->cmbMunicipality->Clear();
         $this->cmbMunicipality->AddItem('--Select Province First--', null , null);
      }
   }




   function btnSubmitClick($sender, $params)
   {
      $this->validateRequired($this->edname->Text, 'lblname');
      $this->validateRequired($this->edAddress->Text, 'lblAddress');
      $this->validateRequired($this->edCity->Text, 'lblCity');
      $error = $this->hfError->getValue();
      if($error == '1' || $number != $key)
      {
         $this->hfError->setValue('0');
         return false;
      }
      else if($error == '0')
      {
         if($this->hiddenComp->getValue() == 0)
         {
            mysql_query("INSERT INTO company_garage(company_id,
       name,
       bldg,
       street,
       city,
       country)

       VALUES
       ('" . $_SESSION['rand'] . "',
       '" . $this->edname->Text . "',
       '" . $this->edBldg->Text . "',
       '" . $this->edAddress->Text . "',
       '" . $this->edCity->Text. "',
       '" . $this->cmbCountry->getItemIndex() . "')")or die("INSERT BRANCH - " . mysql_error());
         }

         if($this->hiddenComp->getValue() != 0)
         {
            mysql_query("INSERT INTO company_garage(company_id,
       name,
       bldg,
       street,
       city,
       country)

       VALUES
       ('" . $this->hiddenComp->getValue() . "',
       '" . $this->edname->Text . "',
       '" . $this->edBldg->Text . "',
       '" . $this->edAddress->Text . "',
       '" . $this->edCity->Text . "',
       '" . $this->cmbCountry->getItemIndex() . "')")or die("INSERT BRANCH - " . mysql_error());
         }

         echo '<script language=javascript>
       alert("Added garage successfully");
       window.opener.JTBranch.location.reload();
       var answer = confirm("Add another record?");
       if(answer)
       {
       window.location.href="comp_garage_create.php?comp_id='.$this->hiddenComp->getValue().'";
       }
       else
       {
       window.close();
       }
       //window.close()
       //window.opener.document.getElementById("JTContactList").src="vendor_contact_list.php";
       </script>';
      }
   }


   function btnContactCancelClick($sender, $params)
   {
      echo '<script language=javascript>
       window.close()</script>';


   }

}

global $application;

global $Add_garage;

//Creates the form
$Add_garage = new Add_garage($application);

//Read from resource file
$Add_garage->loadResource(__FILE__);

//Shows the form
$Add_garage->show();

?>